package com.mt.exception;

import com.mt.constants.Result;
import com.mt.enums.Status;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Wang
 * @Description 全局异常统一处理
 * @date 2020/11/9-15:55
 */

@RestControllerAdvice
@Slf4j
public class GlobalExceptionAdvice {

    @ExceptionHandler(value = GlobalException.class)
    public Result GlobalExceptionHandler(HttpServletRequest req, GlobalException e) throws Exception {
        Result result = new Result<>();
        result.setMsg(e.getMessage());
        result.setCode(e.getStatus().getCode());
        result.setData("ErrorUrl: "+req.getRequestURL().toString());
        return result;
    }

    @ExceptionHandler(value = RuntimeException.class)
    public Result runtimeExceptionHandler(HttpServletRequest req, RuntimeException e) throws Exception {
        Result result = new Result<>();
        log.error(e.getMessage());
        result.setMsg("系统错误:"+ e.getMessage());
        result.setCode(Status.SYS_ERROR.getCode());
        result.setData("ErrorUrl: "+req.getRequestURL().toString());
        return result;
    }



}
